package hotelinteligente.vista;

import hotelinteligente.Dispositivos;
import hotelinteligente.controladores.Controlador;
import java.awt.Dimension;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.Timer;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * VentanaPrincipal.java
 *
 * Created on 20/11/2012, 14:27:08
 */

/**
 *
 * @author vero
 */
public class mainJFrame extends javax.swing.JFrame {

    private JDialogActivarDispositivo dialogActivar;
    private JDialogDesactivarDispositivo dialogDesactivar;
    private JDialogVerCamara dialogVerCamara;
    private JDialogAlarmaIncendio dialogAlarmaIncendio;
    private JDialogCombateIncendio dialogCombateIncendio;
    private JDialogDiscando dialogDiscando;
    private boolean alarma;
    private Sonido sonido;
    private Timer timer;

    /** Creates new form VentanaPrincipal */
    public mainJFrame() {
        initComponents();
        alarma= false;
        centerWindow();
        updatePantalla();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabelTitulo = new javax.swing.JLabel();
        jPanelPasillo1 = new hotelinteligente.vista.JPanelPasillo();
        jButtonDesactivar = new javax.swing.JButton();
        jButtonActivar = new javax.swing.JButton();
        jButtonIncendioCamara1 = new javax.swing.JButton();
        jButtonCamara1 = new javax.swing.JButton();
        jButtonCamara2 = new javax.swing.JButton();
        jButtonDeshabilitarAlerta = new javax.swing.JButton();
        jButtonAlarmaIncendio = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("Vigilancia - Hotel");
        setIconImage(java.awt.Toolkit.getDefaultToolkit().getImage(getClass().getResource("images/logo-icon.png")));
        setResizable(false);

        jLabelTitulo.setText("Dispositivos Activos- Primer Piso");

        javax.swing.GroupLayout jPanelPasillo1Layout = new javax.swing.GroupLayout(jPanelPasillo1);
        jPanelPasillo1.setLayout(jPanelPasillo1Layout);
        jPanelPasillo1Layout.setHorizontalGroup(
            jPanelPasillo1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 640, Short.MAX_VALUE)
        );
        jPanelPasillo1Layout.setVerticalGroup(
            jPanelPasillo1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 353, Short.MAX_VALUE)
        );

        jButtonDesactivar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/hotelinteligente/vista/images/turnoff.png"))); // NOI18N
        jButtonDesactivar.setText("Desactivar Dispositivo");
        jButtonDesactivar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonDesactivarActionPerformed(evt);
            }
        });

        jButtonActivar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/hotelinteligente/vista/images/turnon.png"))); // NOI18N
        jButtonActivar.setText("Activar Dispositivo");
        jButtonActivar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonActivarActionPerformed(evt);
            }
        });

        jButtonIncendioCamara1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/hotelinteligente/vista/images/fire-icon.png"))); // NOI18N
        jButtonIncendioCamara1.setText("SimularIncendioCamara1");
        jButtonIncendioCamara1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonIncendioCamara1ActionPerformed(evt);
            }
        });

        jButtonCamara1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/hotelinteligente/vista/images/camara-seguridad-icono.png"))); // NOI18N
        jButtonCamara1.setText("Ver Camara 1");
        jButtonCamara1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonCamara1ActionPerformed(evt);
            }
        });

        jButtonCamara2.setIcon(new javax.swing.ImageIcon(getClass().getResource("/hotelinteligente/vista/images/camara-seguridad-icono.png"))); // NOI18N
        jButtonCamara2.setText("Ver Camara 2");
        jButtonCamara2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonCamara2ActionPerformed(evt);
            }
        });

        jButtonDeshabilitarAlerta.setIcon(new javax.swing.ImageIcon(getClass().getResource("/hotelinteligente/vista/images/alarma-icon.png"))); // NOI18N
        jButtonDeshabilitarAlerta.setText("Deshabilitar Alerta");
        jButtonDeshabilitarAlerta.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonDeshabilitarAlertaActionPerformed(evt);
            }
        });

        jButtonAlarmaIncendio.setIcon(new javax.swing.ImageIcon(getClass().getResource("/hotelinteligente/vista/images/matafuego-icon.png"))); // NOI18N
        jButtonAlarmaIncendio.setText("Alarma Incendio");
        jButtonAlarmaIncendio.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonAlarmaIncendioActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabelTitulo)
                .addContainerGap(664, Short.MAX_VALUE))
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(jPanelPasillo1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                    .addComponent(jButtonDesactivar, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jButtonCamara1, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jButtonCamara2, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jButtonDeshabilitarAlerta, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jButtonIncendioCamara1, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jButtonActivar, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jButtonAlarmaIncendio, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabelTitulo)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(42, 42, 42)
                        .addComponent(jButtonActivar)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jButtonDesactivar)
                        .addGap(40, 40, 40)
                        .addComponent(jButtonCamara1)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jButtonCamara2)
                        .addGap(35, 35, 35)
                        .addComponent(jButtonDeshabilitarAlerta)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jButtonAlarmaIncendio)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(jButtonIncendioCamara1))
                    .addGroup(layout.createSequentialGroup()
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(jPanelPasillo1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents
    
    private void centerWindow() {
        //Center the window
        Dimension screenSize= Toolkit.getDefaultToolkit().getScreenSize();
        Dimension frameSize= getSize();
        if(frameSize.height > screenSize.height) {
            frameSize.height = screenSize.height;
        }
        if(frameSize.width > screenSize.width) {
            frameSize.width = screenSize.width;
        }
        setLocation((screenSize.width - frameSize.width) / 2, (screenSize.height - frameSize.height) / 2);
    }

    private void jButtonActivarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonActivarActionPerformed
        this.dialogActivar= new JDialogActivarDispositivo(this, rootPaneCheckingEnabled);
        this.dialogActivar.setLocationRelativeTo(this);
        this.dialogActivar.setVisible(true);
    }//GEN-LAST:event_jButtonActivarActionPerformed

    private void jButtonDesactivarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonDesactivarActionPerformed
        this.dialogDesactivar= new JDialogDesactivarDispositivo(this, rootPaneCheckingEnabled);
        this.dialogDesactivar.setLocationRelativeTo(this);
        this.dialogDesactivar.setVisible(true);
    }//GEN-LAST:event_jButtonDesactivarActionPerformed

    private void jButtonIncendioCamara1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonIncendioCamara1ActionPerformed
        Controlador.getInstancia().generarAlarmaIncendio();
    }//GEN-LAST:event_jButtonIncendioCamara1ActionPerformed

    private void jButtonCamara1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonCamara1ActionPerformed
        Controlador.getInstancia().mostrarCamara(Dispositivos.ID_CAMARA1);
    }//GEN-LAST:event_jButtonCamara1ActionPerformed

    private void jButtonCamara2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonCamara2ActionPerformed
        Controlador.getInstancia().mostrarCamara(Dispositivos.ID_CAMARA2);
    }//GEN-LAST:event_jButtonCamara2ActionPerformed

    private void jButtonDeshabilitarAlertaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonDeshabilitarAlertaActionPerformed
        sonido.stop();
        alarma= false;
        updatePantalla();
    }//GEN-LAST:event_jButtonDeshabilitarAlertaActionPerformed

    private void jButtonAlarmaIncendioActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonAlarmaIncendioActionPerformed
        Controlador.getInstancia().alarmaIncendio();
    }//GEN-LAST:event_jButtonAlarmaIncendioActionPerformed
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButtonActivar;
    private javax.swing.JButton jButtonAlarmaIncendio;
    private javax.swing.JButton jButtonCamara1;
    private javax.swing.JButton jButtonCamara2;
    private javax.swing.JButton jButtonDesactivar;
    private javax.swing.JButton jButtonDeshabilitarAlerta;
    private javax.swing.JButton jButtonIncendioCamara1;
    private javax.swing.JLabel jLabelTitulo;
    private hotelinteligente.vista.JPanelPasillo jPanelPasillo1;
    // End of variables declaration//GEN-END:variables

    public void agregarDispositivoDibujable(String id, String ruta, int x, int y) {
        PisoDibujable piso= jPanelPasillo1.getPiso();
        piso.agregarDispositivo(id, ruta, x, y);
        updatePantalla();
    }

    public void dibujarDispositivo(String id) {
        DispositivoDibujable dispositivo= jPanelPasillo1.getPiso().getDispositivoDibujable(id);
        if(dispositivo != null) {
            dispositivo.dibujar();
            updatePantalla();
        }
    }

    public void noDibujarDispositivo(String id) {
        DispositivoDibujable dispositivo= jPanelPasillo1.getPiso().getDispositivoDibujable(id);
        if(dispositivo != null) {
            dispositivo.no_dibujar();
            updatePantalla();
        }
    }

    private void updateBotones() {
        PisoDibujable piso= jPanelPasillo1.getPiso();
        int max= piso.getCantidadDispositivos();
        int activos= piso.getCantidadDibujados();

        if(activos == max)
            jButtonActivar.setEnabled(false);
        else
            jButtonActivar.setEnabled(true);

        if(activos == 0)
            jButtonDesactivar.setEnabled(false);
        else
            jButtonDesactivar.setEnabled(true);

        //CAMARA
        jButtonCamara1.setVisible(false);
        jButtonCamara2.setVisible(false);

        for(int i= 1; i<=2; i++) {
            DispositivoDibujable d= jPanelPasillo1.getPiso().getDispositivoDibujable("Camara"+i);
            if(d != null) {
                if(d.getDibujar() && i==1)
                    jButtonCamara1.setVisible(true);
                if(d.getDibujar() && i==2)
                    jButtonCamara2.setVisible(true);
            }
        }

        //SIMULAR
        DispositivoDibujable d= jPanelPasillo1.getPiso().getDispositivoDibujable(Dispositivos.ID_DETECTOR_HUMO1);
        jButtonIncendioCamara1.setEnabled(false);
        if(d != null)
            if(d.getDibujar())
                jButtonIncendioCamara1.setEnabled(true);

        //ALARMA
        if(alarma) {
            jButtonDeshabilitarAlerta.setVisible(true);
            jButtonAlarmaIncendio.setVisible(true);
        } else {
            jButtonDeshabilitarAlerta.setVisible(false);
            jButtonAlarmaIncendio.setVisible(false);
        }
    }

    public PisoDibujable getPiso() {
        return jPanelPasillo1.getPiso();
    }

    private void updatePantalla() {
        updateBotones();
        validate();
        repaint();
        jPanelPasillo1.revalidate();
        jPanelPasillo1.repaint();
        jPanelPasillo1.refresh(this.getGraphics());
    }

    public void mostrarCamara(String id) {
        this.dialogVerCamara= new JDialogVerCamara(this, rootPaneCheckingEnabled, id);
        this.dialogVerCamara.setLocationRelativeTo(this);
        this.dialogVerCamara.setVisible(true);
    }

    public void mostrarAlertaIncendio() {
        sonido= new Sonido();
        sonido.reproducirAlerta();
        alarma= true;
        jButtonDeshabilitarAlerta.setVisible(true);
        jButtonAlarmaIncendio.setVisible(true);

        this.dialogAlarmaIncendio= new JDialogAlarmaIncendio(this, rootPaneCheckingEnabled);
        this.dialogAlarmaIncendio.setLocationRelativeTo(this);
        this.dialogAlarmaIncendio.setVisible(true);
    }

    public void alarmaIncendio() {
        sonido.stop();
        sonido= new Sonido();
        sonido.reproducirAlertaIncendio();
        mostrarMensajeAspersor();
        mostrarMensajeDiscando();
    }

    private void mostrarMensajeAspersor() {    
        timer= new Timer(4000, new ActionListener() {
            public void actionPerformed(ActionEvent ae) {
                dialogCombateIncendio.dispose();
            }
        });
        dialogCombateIncendio= new JDialogCombateIncendio(this, alarma);
        dialogCombateIncendio.setLocationRelativeTo(this);
        timer.start();
        dialogCombateIncendio.setVisible(true);
    }

    private void mostrarMensajeDiscando() {
        timer= new Timer(4000, new ActionListener() {
            public void actionPerformed(ActionEvent ae) {
                dialogDiscando.dispose();
            }
        });
        dialogDiscando= new JDialogDiscando(this, alarma);
        dialogDiscando.setLocationRelativeTo(this);
        timer.start();
        dialogDiscando.setVisible(true);
    }
}
